package com.chenyue.cm.moudules.system.service.impl;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.moudules.system.bean.TreeConfigBean;
import com.chenyue.cm.moudules.system.service.SysMenusService;
import com.chenyue.cm.moudules.system.utils.CmTreeUtils;
import com.chenyue.cm.system.dao.SysMenusMapper;
import com.chenyue.cm.system.dao.SysRoleMenusMapper;
import com.chenyue.cm.system.domain.SysMenus;
import com.chenyue.cm.system.domain.SysMenusExample;
import com.chenyue.cm.system.domain.SysRoleMenus;
import com.chenyue.cm.system.domain.SysRoleMenusExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "menuService")
@Transactional
public class SysMenusServiceImpl extends BaseServiceImpl<SysMenus> implements SysMenusService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysMenusMapper menusMapper;

    @Resource
    private SysRoleMenusMapper sysRoleMenusMapper;
    
    @Override
    @Resource(name = "sysMenusMapper")
    public void setBaseMapper(LcBaseMapper<SysMenus, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }
    
    @Override
	@Resource(name = "sysMenusMapper")
	public void setBaseDao(BaseMapper<SysMenus> baseDao) {
		this.baseDao = baseDao;
	}
    
	@Override
	public List<Map<String, Object>> getMainMenuByRoleId(String roleid) {
		
		
		//获取树形结构的所有菜单信息
		List<Map<String, Object>> resultMapList  = new ArrayList<Map<String, Object>>();
		
		//获取所有菜单数据
		SysMenusExample sysMenusExample = new SysMenusExample();
		sysMenusExample.createCriteria().andTypeNotEqualTo(3);
		sysMenusExample.setOrderByClause("order_no asc");
		List<SysMenus> allMenu = menusMapper.selectByExample(sysMenusExample);
		
		
		if ("0".equals(roleid)) {//超级管理员，拥有所有权限
			
			//获取树形结构菜单信息
			TreeConfigBean treeConfigBean = new TreeConfigBean();
			treeConfigBean.setChildListLabelText("submenus"); 
			treeConfigBean.setUncheckIdsHiddenFlag(true); 
			//获取树形结构的所有菜单信息
			resultMapList  =  CmTreeUtils.getTreeList(allMenu , treeConfigBean);
			
		}else {//普通用户，权限绑定
			
			SysRoleMenusExample e1 = new SysRoleMenusExample();
			e1.createCriteria().andRoleIdEqualTo(roleid);
			List<SysRoleMenus> rolemenuList = sysRoleMenusMapper.selectByExample(e1);
			// 获取该用户角色对应的菜单权限
			List<String> checkedlist = new ArrayList<String>();
			for (SysRoleMenus rm : rolemenuList) {
				String ridString = rm.getMenuId();
				checkedlist.add(ridString);
			}
			
			
			//获取树形结构菜单信息
			TreeConfigBean treeConfigBean = new TreeConfigBean();
			treeConfigBean.setChildListLabelText("submenus"); 
			treeConfigBean.setUncheckIdsHiddenFlag(true); 
			resultMapList  =  CmTreeUtils.getCheckedTreeList(allMenu ,treeConfigBean , checkedlist );
			
		}
		
		return resultMapList;
		
	}
	 
   
}
